package com.android.gilastv.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.GlideUtil;
import com.android.baselibrary.util.Util;
import com.android.gilastv.R;
import com.android.gilastv.adapter.CommVideoListAdapter;
import com.android.gilastv.bean.CommVideoInfo;
import com.android.gilastv.constants.Constants;
import com.android.gilastv.event.Event_VideoDetail_PlayVideo;
import com.android.gilastv.manager.API_Manager;
import com.android.gilastv.manager.UserManager;
import com.android.gilastv.ui.view.Comm_HeadStateView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoDetailActivity extends BaseActivity {

    @BindView(R.id.iv_cover)
    ImageView iv_cover;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_des)
    TextView tv_des;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.iv_dianzan)
    ImageView iv_dianzan;
    @BindView(R.id.iv_shoucang)
    ImageView iv_shoucang;
    @BindView(R.id.head_stateview)
    Comm_HeadStateView head_stateview;


    CommVideoListAdapter adapter;
    List<CommVideoInfo> list;

    String id;  //视频ID
    CommVideoInfo videoInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_detail;
    }

    @Override
    protected void initView() {
        head_stateview.setLeftClickFinish(this);
        id = getIntent().getStringExtra("id");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerview.setLayoutManager(linearLayoutManager);
//        recyclerview.addItemDecoration(new GridSpacingItemDecoration(0, Util.dip2px(mContext,10),true));
        adapter = new CommVideoListAdapter(mContext, null);
        adapter.setWidth(Util.dip2px(mContext, 130));
        recyclerview.setAdapter(adapter);

    }

    @Override
    protected void initData() {
        getDetail();
        getVideoRecommend();
    }

    //获取详情
    private void getDetail() {
        API_Manager.getVideoInfo(mContext, id, new OkHttpCallBack<BaseResponce<CommVideoInfo>>() {
            @Override
            public void onSuccess(BaseResponce<CommVideoInfo> baseResponce) {
                videoInfo = baseResponce.getData();
                bindData();
            }

            @Override
            public void onFailure(BaseResponce<CommVideoInfo> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //获取相关推荐
    private void getVideoRecommend() {
        API_Manager.getVideoRecommend(mContext, id, new OkHttpCallBack<BaseResponce<List<CommVideoInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<List<CommVideoInfo>> baseResponce) {
                adapter.setData(baseResponce.getData());
            }

            @Override
            public void onFailure(BaseResponce<List<CommVideoInfo>> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }


    private void bindData() {
        GlideUtil.displayImage(mContext, videoInfo.getCoverUrl(videoInfo.coverId), iv_cover, 0);
        tv_name.setText(videoInfo.hanyuName);
        tv_des.setText(videoInfo.synopsis);

        if (videoInfo.likeFlag) {
            iv_dianzan.setImageResource(R.drawable.ico_dianzan_red);
        } else {
            iv_dianzan.setImageResource(R.drawable.ico_dianzan_black);
        }
        if (videoInfo.collectFlag) {
            iv_shoucang.setImageResource(R.drawable.ico_shoucang_red);
        } else {
            iv_shoucang.setImageResource(R.drawable.ico_shoucang_black);
        }
    }

    @OnClick({R.id.ll_collect, R.id.ll_dianan, R.id.ll_play})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_collect:     //add，del
                if (UserManager.isLoginAndShowLogin(mContext)) {
                    if (videoInfo.collectFlag) {
                        userCollect("del");
                    } else {
                        userCollect("add");
                    }
                }
                break;
            case R.id.ll_dianan:
                if (UserManager.isLoginAndShowLogin(mContext)) {
                    if (videoInfo.likeFlag) {
                        userLike("del");
                    } else {
                        userLike("add");
                    }
                }
                break;
            case R.id.ll_play:   //播放
                Event_VideoDetail_PlayVideo event = new Event_VideoDetail_PlayVideo();
                event.videoInfo = videoInfo;
                EventBus.getDefault().post(event);
                break;
        }
    }

    //收藏
    private void userCollect(String type) {
        API_Manager.userCollect(mContext, type, videoInfo.id, new OkHttpCallBack<BaseResponce<Object>>() {
            @Override
            public void onSuccess(BaseResponce<Object> baseResponce) {
                CommToast.showToast(mContext, "已收藏");
                getDetail();
            }

            @Override
            public void onFailure(BaseResponce<Object> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //点赞
    private void userLike(String type) {
        API_Manager.userLike(mContext, type, videoInfo.id, new OkHttpCallBack<BaseResponce<Object>>() {
            @Override
            public void onSuccess(BaseResponce<Object> baseResponce) {
                CommToast.showToast(mContext, "已收藏");
                getDetail();
            }

            @Override
            public void onFailure(BaseResponce<Object> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }
}
